package org.spring.springboot.app.agent.withdraw.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.agent.balancelog.domain.po.SysOfficeBalanceLogPO;
import org.spring.springboot.app.agent.balancelog.service.ISysOfficeBalanceLogService;
import org.spring.springboot.app.agent.withdraw.dao.AccountWithdrawMapper;
import org.spring.springboot.app.agent.withdraw.domain.po.AccountWithdrawPO;
import org.spring.springboot.app.agent.withdraw.domain.vo.AccountWithdrawVO;
import org.spring.springboot.app.agent.withdraw.domain.vo.SeachWithdrawVO;
import org.spring.springboot.app.agent.withdraw.service.IAccountWithdrawService;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.sys.office.dao.SysOfficeDao;
import org.spring.springboot.app.sys.office.domain.po.SysOfficeWalletPO;
import org.spring.springboot.app.user.balancelog.domain.po.SysUserBalanceLogPO;
import org.spring.springboot.app.user.balancelog.service.ISysUserBalanceLogService;
import org.spring.springboot.app.user.sysuser.dao.SysUserDao;
import org.spring.springboot.app.user.sysuser.domain.po.SysUserWalletPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class AccountWithdrawServiceImpl implements IAccountWithdrawService {

    @Autowired
    private AccountWithdrawMapper dao;
    @Autowired
    private SysOfficeDao sysOfficeDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private ISysOfficeBalanceLogService sysOfficeBalanceLogService;
    @Autowired
    private ISysUserBalanceLogService sysUserBalanceLogService;


    @Override
    public List<AccountWithdrawPO> findAll(SeachWithdrawVO seachVo){
        return dao.findAll(seachVo,seachVo.getPageNum(), seachVo.getPageSize());
    }

    @Override
    public  AccountWithdrawPO find(String id){
        return  dao.selectByPrimaryKey(id);
    }
    @Override
    @Transactional
    public R save(AccountWithdrawVO vo){
        R r = new R();
        AccountWithdrawPO po = new AccountWithdrawPO();
        BeanUtils.copyProperties(vo, po);

        BigDecimal dealMoney = po.getMoney();
        if(po.getOthermoney()==null ){
            po.setOthermoney(BigDecimal.ZERO);
        }
        dealMoney=dealMoney.add(po.getOthermoney()); //操作金额
        po.preInsert();

        if(StringUtils.isNotBlank(po.getUserId())){
            // 用户提现 需要扣除用户金额
            SysUserWalletPO walletPO = sysUserDao.queryWallet(po.getUserId());
            po.setRemainmoney(walletPO.getBalance());//填充操作前当前余额
            if (dealMoney.compareTo(walletPO.getBalance()) > 0) {
                return  r.fail().setMessage("余额不足");
            }

            walletPO.setOldBalance(walletPO.getBalance());
            walletPO.setBalance(walletPO.getBalance().subtract(dealMoney));
            if (sysUserDao.updateWallet(walletPO) <1 ) {
                throw new RuntimeException("扣除余额失败");
            }
            SysUserBalanceLogPO balanceLogPO = new SysUserBalanceLogPO();
            balanceLogPO.setUserId(po.getUserId());
            balanceLogPO.setType(Constants.SYS_OFFICE_BALANCE_LOG_TYPE_101);
            balanceLogPO.setMoney(dealMoney);
            balanceLogPO.setRelateId(po.getId());
            balanceLogPO.preInsert();
            if(sysUserBalanceLogService.save(balanceLogPO)<1){
                throw new RuntimeException("钱包日志添加失败");
            }
        }
        if(StringUtils.isNotBlank(po.getOfficeId())){
            //代理商提现 需要扣除代理商金额
            SysOfficeWalletPO walletPO = sysOfficeDao.queryWallet(po.getOfficeId());
            po.setRemainmoney(walletPO.getBalance());//填充操作前当前余额
            if (dealMoney.compareTo(walletPO.getBalance()) > 0) {
                return  r.fail().setMessage("余额不足");
            }
            walletPO.setOldBalance(walletPO.getBalance());
            walletPO.setBalance(walletPO.getBalance().subtract(dealMoney));
            if (sysOfficeDao.updateWallet(walletPO) <1 ) {
                throw new RuntimeException("扣除余额失败");
            }
            //添加机构钱包日志
            SysOfficeBalanceLogPO balanceLogPO = new SysOfficeBalanceLogPO();
            balanceLogPO.setOfficeId(walletPO.getId());
            balanceLogPO.setType(Constants.SYS_OFFICE_BALANCE_LOG_TYPE_101);
            balanceLogPO.setMoney(dealMoney);
            balanceLogPO.setRelateId(po.getId());
            balanceLogPO.preInsert();
            if(sysOfficeBalanceLogService.save(balanceLogPO)<1){
                throw new RuntimeException("钱包日志添加失败");
            }
        }
        if(dao.insertSelective(po)<1){
            throw new RuntimeException("添加提现信息失败");
        }
        return r.success();
    }
    @Override
    @Transactional
    public R update(AccountWithdrawVO vo){
        R r = new R();
        if(StringUtils.isBlank(vo.getId())){
            return  r.fail().setMessage("ID不能为空！");
        }
        AccountWithdrawPO oldPo=dao.selectByPrimaryKey(vo.getId());

        AccountWithdrawPO po = new AccountWithdrawPO();
        BeanUtils.copyProperties(vo, po);
        if(po.getState()!=null&&po.getState()!=oldPo.getState()){
            if(po.getState().toString().equals("2")){
                BigDecimal dealMoney = oldPo.getMoney();
                if(oldPo.getOthermoney()==null ){
                    oldPo.setOthermoney(BigDecimal.ZERO);
                }
                dealMoney=dealMoney.add(oldPo.getOthermoney()); //操作金额
                //拒绝退款需要把提款金额返还给提款人
                if(StringUtils.isNotBlank(oldPo.getUserId())){
                    //代理商提现 需要返款给代理商
                    SysUserWalletPO walletPO = sysUserDao.queryWallet(oldPo.getUserId());
                    walletPO.setOldBalance(walletPO.getBalance());
                    walletPO.setBalance(walletPO.getBalance().add(dealMoney));
                    if (sysUserDao.updateWallet(walletPO) <1 ) {
                        throw new RuntimeException("扣除余额失败");
                    }
                    //添加机构钱包日志
                    SysUserBalanceLogPO balanceLogPO = new SysUserBalanceLogPO();
                    balanceLogPO.setUserId(oldPo.getUserId());
                    balanceLogPO.setType(Constants.SYS_USER_BALANCE_LOG_TYPE_102);
                    balanceLogPO.setMoney(dealMoney);
                    balanceLogPO.setRelateId(po.getId());
                    balanceLogPO.preInsert();
                    if(sysUserBalanceLogService.save(balanceLogPO)<1){
                        throw new RuntimeException("钱包日志添加失败");
                    }
                }
                if(StringUtils.isNotBlank(oldPo.getOfficeId())){
                    //代理商提现 需要返款给代理商
                    SysOfficeWalletPO walletPO = sysOfficeDao.queryWallet(oldPo.getOfficeId());
                    walletPO.setOldBalance(walletPO.getBalance());
                    walletPO.setBalance(walletPO.getBalance().add(dealMoney));
                    if (sysOfficeDao.updateWallet(walletPO) <1 ) {
                        throw new RuntimeException("添加余额失败");
                    }
                    //添加机构钱包日志
                    SysOfficeBalanceLogPO balanceLogPO = new SysOfficeBalanceLogPO();
                    balanceLogPO.setOfficeId(walletPO.getId());
                    balanceLogPO.setType(Constants.SYS_OFFICE_BALANCE_LOG_TYPE_108);
                    balanceLogPO.setMoney(dealMoney);
                    balanceLogPO.setRelateId(po.getId());
                    balanceLogPO.preInsert();
                    if(sysOfficeBalanceLogService.save(balanceLogPO)<1){
                        throw new RuntimeException("钱包日志添加失败");
                    }
                }
            }
        }
        po.preUpdate();
         if(dao.updateByPrimaryKeySelective(po)<1){
             throw new RuntimeException("修改操作失败！");
         }
        return r.success();
    }
}
